
import { router, asyncRouterMap } from '@/router'


/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  // roles为权限身份数组
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.indexOf(role) >= 0)
  }
  else {
    return true
  }
}

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param asyncRouterMap
 * @param roles
 */
function filterAsyncRouter(asyncRouterMap, roles) {
  // 返回满足条件的子路由对象
  const accessedRouters = asyncRouterMap.filter(route => {
    if (hasPermission(roles, route)) {
      if (route.children && route.children.length) {
        // route.children重新过滤赋值;
        route.children = filterAsyncRouter(route.children, roles)
      }
      return true // 返回该权限路由对象;
    }
    return false
  });
  return accessedRouters
}

const permission = {
  state: {
    routers: router,
    addRouters: [],
  },
  getters: {
    permission_routers: state => state.routers, // 所有路由
    addRouters: state => state.addRouters,      // 权限过滤路由
  },
  mutations: {

    SET_Routers: (state, routers) => {
      state.addRouters = routers;
      state.routers = router.concat(routers);
    },



  },
  actions: {

    // 根据角色，重新设置权限路由；
    GenerateRoutes({ commit }, data) {

      return new Promise(resolve => {

        let roles = data.roles;
        let routers = '';

        // 如果是管理员，直接将权限路有赋值给新路由
        if (roles.indexOf('admin') > 0) {
          routers = asyncRouterMap;
        }
        else { // 非管理员，需要筛选权限
          routers = filterAsyncRouter(asyncRouterMap, roles);
        }

        // 添加路由
        commit('SET_Routers', routers);

        resolve();
      })
    },




  },
};

export default permission;
